package com.smz.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.smz.entity.TProductOrder;
import com.smz.service.TProductOrderService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class TProductOrderController {
    
    @Autowired
    private TProductOrderService tProductOrderService;
    @Autowired
    private TProductOrderService productOrderService;

    @RequestMapping(value = "/tProductOrder/{current}/{rows}",method = RequestMethod.GET)
    public ResponseEntity<Page<TProductOrder>> pageTProductOrder(@PathVariable(value = "current")Integer current,@PathVariable("rows")Integer rows){
        Page<TProductOrder> pageTProductOrder = tProductOrderService.findPageTProductOrder(current, rows);
        return ResponseEntity.ok(pageTProductOrder);
    }

    @RequestMapping(value = "/tProductOrder/user",method = RequestMethod.GET)
    public ResponseEntity<User> findUser(){
        User principal = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        return ResponseEntity.ok(principal);
    }
    @RequestMapping(value = "/tProductOrder",method = RequestMethod.PUT)
    public String modifyUser(@RequestBody TProductOrder tpo){
        tpo.setOrderStatus("-3");
        tProductOrderService.update(tpo,new QueryWrapper<TProductOrder>().lambda().eq(TProductOrder::getId,tpo.getId()));
        return "ok";
    }
    @GetMapping("/productOrders")
    public ResponseEntity<List<TProductOrder>> findAllProOrders(){
        List<TProductOrder> allOrders = productOrderService.findAllOrders();
        return ResponseEntity.ok(allOrders);
    }


    @GetMapping("/productOrder/{id}")
    public ResponseEntity<TProductOrder> findProOrderById(@PathVariable("id") Integer id){
        TProductOrder order = productOrderService.findOrderById(id);
        return ResponseEntity.ok(order);
    }

    @ApiOperation("根据订单编号模糊查询")
    @GetMapping("/TProductOrder")
    public ResponseEntity<Page<TProductOrder>> selectTProductOrderByLike(@RequestParam("id") Integer id,@RequestParam("current") Integer current,@RequestParam("rows") Integer rows){
        Page<TProductOrder> tProductOrderById = productOrderService.findTProductOrderById(current, rows, id);
        return ResponseEntity.ok(tProductOrderById);
    }
}
